package com.sentiment.bean;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sentiment.database.bean.Permission;
import com.sentiment.database.bean.Role;
import com.sentiment.database.bean.Role_Permissions;
import com.sentiment.database.bean.User;
import com.sentiment.database.dbUtil.DbUtil;

public class RoleList {

	
	private String role_id;
	
	private String role_detail;
	
	private List<String> per_id;
	
	private List<String> per_detail;
	
	public String getRole_id() {
		return role_id;
	}

	public void setRole_id(String role_id) {
		this.role_id = role_id;
	}

	public String getRole_detail() {
		return role_detail;
	}

	public void setRole_detail(String role_detail) {
		this.role_detail = role_detail;
	}

	public List<String> getPer_detail() {
		return per_detail;
	}

	public void setPer_detail(List<String> per_detail) {
		this.per_detail = per_detail;
	}

	public List<String> getPer_id() {
		return per_id;
	}

	public void setPer_id(List<String> per_id) {
		this.per_id = per_id;
	}
	
	public RoleList(String role_id, String role_detail, List<String> per_id,
			List<String> per_detail) {
		super();
		this.role_id = role_id;
		this.role_detail = role_detail;
		this.per_id = per_id;
		this.per_detail = per_detail;
	}

	@Override
	public String toString() {
		return "RoleList [role_id=" + role_id + ", role_detail=" + role_detail
				+ ", per_id=" + per_id + ", per_detail=" + per_detail + "]";
	}

	public RoleList() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 分页获取 RoleList
	 * @param page 页数
	 * @param count 每页个数
	 * @return
	 */
	public List<RoleList> getRoleList(int page, int count) {
		
		List<RoleList> roles = new ArrayList<>();
		
		DbUtil dbutil = new DbUtil();
		dbutil.openConnection(Role.dbName, Role.collection);
		
		List<Document> trolelist = dbutil.getPage(page, count);		
		dbutil.closeConnection();
		
		Document trole = new Document();
		
		Role role = new Role();
		String role_id = "";
		String per_detail = "";
		Permission per = new Permission();
		List<Permission> perlist = new ArrayList<>();
		
		
		for(int i = 0 ; i < trolelist.size(); i++){
			
			RoleList rolelist = new RoleList();
			List<String> per_detaillist = new ArrayList<>();
			trole = trolelist.get(i);			
			role = new Role(trole);			
			role_id = role.get_id();
			
			rolelist.setRole_id(role_id);
			rolelist.setRole_detail(trole.getString("role_detail"));
			
			Role_Permissions role_pers = new Role_Permissions();
			role_pers =  role_pers.getRole_PermissionsByRole_id(role_id);
			rolelist.setPer_id(role_pers.getPermission_id());
			perlist = role_pers.getPermissionListByRole_id(role_id);
			
			for(int j = 0; j < perlist.size(); j++){
				
				per = perlist.get(j);
				per_detail = per.getPer_detail();
				per_detaillist.add(per_detail);
			}
			rolelist.setPer_detail(per_detaillist);
			roles.add(rolelist);			
		}
		return roles;
		
	}
	
	
	/**
	 * 返回 Role 记录条数
	 * @return
	 */
	@JsonIgnore
	public long getCount(){
		
		return new Role().getCount();		
	}
	
	/**
	 * 根据 Role_Permission 返回 RoleList
	 * @param role_pers
	 * @return
	 */
	public RoleList getRoleListByRole_pers(Role_Permissions role_pers){
		
		RoleList rolelist = new RoleList();
		String role_id = role_pers.getRole_id();
		List<String> pers_id = role_pers.getPermission_id(); 
		rolelist.setRole_id(role_id);
		rolelist.setPer_id(pers_id);
		
		Role role = new Role();
		role = role.getRoleByRoleId(role_id);
		rolelist.setRole_detail(role.getRole_detail());
		
		List<String> per_detail = new ArrayList<>();
		for (int i = 0; i < pers_id.size(); i++) {
			Permission per = new Permission();
			per = per.getPermissionByPermissionId(pers_id.get(i));
			per_detail.add(per.getPer_detail());
		}
		rolelist.setPer_detail(per_detail);
		
		return rolelist;
	}
	
	
	
	public static void main(String[] args) {
		
		RoleList role = new RoleList();
		List<RoleList> roles = role.getRoleList(1, 10);
		
		System.out.println(roles.size());
		for(int i = 0; i < roles.size(); i++){
			System.out.println(roles.get(i).toString());
		}
		
		ObjectMapper mapper = new ObjectMapper();
		String jsonArray = "";
		try {
			jsonArray = mapper.writeValueAsString(roles);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		String jsonStr = "\"roles\":" + jsonArray;
		System.out.println(jsonStr);
	}
	
	
}
